package it.univaq.di.helpdesk.presentation;

import it.univaq.di.helpdesk.business.model.Device;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;

public class DeviceForm extends ValidatorForm {
	private String deviceType;
	
	private int deviceID;
	private String type;
	private String name;
	private String model;
	private String vendor;
	private String size;
	
	private int hwSize;
	private int swSize;
	
	private List<Device> software;
	private List<Device> hardware;
	
	public DeviceForm() {
		super();
		
		this.hwSize = 0;
		this.swSize = 0;
		
		this.hardware = new ArrayList<Device>();
		this.software = new ArrayList<Device>();

		for (int i=0; i<10; i++) {
			Device hardware = Device.getInstanceOf("Hardware");
			Device software = Device.getInstanceOf("Software");
			hardware.setType("Hardware");
			software.setType("Software");
			
			/* Inizializzo a "vuoto" gli elementi della collection non precaricati
			 * nella form per poter effettuare la validazione
			 */
			if (i >= 1) {
				software.setModel("empty");
				software.setName("empty");
				software.setType("empty");
				software.setVendor("empty");
			}
			
			if (i >= 5) {
				hardware.setModel("empty");
				hardware.setName("empty");
				hardware.setType("empty");
				hardware.setVendor("empty");
			}
			
			this.hardware.add(hardware);
			this.software.add(software);
		}
	}
	
	public int getHwSize() {
		return this.hwSize;
	}

	public int getSwSize() {
		return this.swSize;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public int getDeviceID() {
		return deviceID;
	}

	public void setDeviceID(int deviceID) {
		this.deviceID = deviceID;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getVendor() {
		return vendor;
	}

	public void setVendor(String vendor) {
		this.vendor = vendor;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public List<Device> getSoftware() {
		return software;
	}
	
	public void setSoftware(List<Device> software) {
		this.software = software;
		this.swSize = software.size();
		Collections.sort(this.software);
	}
	
	public List<Device> getHardware() {
		return hardware;
	}
	
	public void setHardware(List<Device> hardware) {
		this.hardware = hardware;
		this.hwSize = hardware.size();
		Collections.sort(this.hardware);
	}

	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);
		this.setDeviceType("Computer");
	}
}
